package com.sls.game.parser.enfu;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.enfu.EnfuTradeResponseInfo;

/**
 * 
 * @description 恩付交易响应消息
 * @author Wangxuegang
 * @date 2013-5-3
 */
public class EnfuTradeResponse {
	private static Logger log = Logger.getLogger(EnfuTradeResponse.class);

	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static EnfuTradeResponseInfo parseTrade(String xmlStr) {
		EnfuTradeResponseInfo info = new EnfuTradeResponseInfo();
		InputSource source = null;
		StringReader read = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);

			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("apiresult".equals(node.getNodeName())) { // 第二层节点
					Element apiresultNode = (Element) node.getChildNodes();
					NodeList apiresultNodes = apiresultNode.getChildNodes();
					for (int j = 0; j < apiresultNodes.getLength(); j++) {
						Node grandsonNode = apiresultNodes.item(j);
						if ("resultcode".equals(grandsonNode.getNodeName())) {
							String resultcode = grandsonNode.getTextContent();
							info.setResultcode(resultcode);
						} else if ("resultdescription".equals(grandsonNode
								.getNodeName())) {
							String resultdescription = grandsonNode
									.getTextContent();
							info.setResultdescription(resultdescription);
						}
					}
				} else if ("buycard".equals(node.getNodeName())) {
					Element buycardNode = (Element) node.getChildNodes();
					NodeList buycardNodes = buycardNode.getChildNodes();
					for (int j = 0; j < buycardNodes.getLength(); j++) {
						Node grandsonNode = buycardNodes.item(j);
						if ("merchantid".equals(grandsonNode.getNodeName())) {
							String merchantid = grandsonNode.getTextContent();
							info.setMerchantid(merchantid);
						} else if ("productid".equals(grandsonNode
								.getNodeName())) {
							String productid = grandsonNode.getTextContent();
							info.setProductid(productid);
						} else if ("producttype".equals(grandsonNode
								.getNodeName())) {
							String producttype = grandsonNode.getTextContent();
							info.setProducttype(producttype);
						} else if ("buynumber".equals(grandsonNode
								.getNodeName())) {
							String buynumber = grandsonNode.getTextContent();
							info.setBuynumber(buynumber);
						} else if ("buyprice"
								.equals(grandsonNode.getNodeName())) {
							String buyprice = grandsonNode.getTextContent();
							info.setBuyprice(buyprice);
						} else if ("orderno".equals(grandsonNode.getNodeName())) {
							String orderno = grandsonNode.getTextContent();
							info.setOrderno(orderno);
						} else if ("userip".equals(grandsonNode.getNodeName())) {
							String userip = grandsonNode.getTextContent();
							info.setUserip(userip);
						} else if ("saleorderid".equals(grandsonNode
								.getNodeName())) {
							String saleorderid = grandsonNode.getTextContent();
							info.setSaleorderid(saleorderid);
						}
					}
				} else if ("chargeinfo".equals(node.getNodeName())) {
					Element chargeinfoNode = (Element) node.getChildNodes();
					NodeList chargeinfoNodes = chargeinfoNode.getChildNodes();
					for (int j = 0; j < chargeinfoNodes.getLength(); j++) {
						Node grandsonNode = chargeinfoNodes.item(j);
						if ("username".equals(grandsonNode.getNodeName())) {
							String username = grandsonNode.getTextContent();
							info.setUsername(username);
						} else if ("gamename"
								.equals(grandsonNode.getNodeName())) {
							String gamename = grandsonNode.getTextContent();
							info.setGamename(gamename);
						} else if ("areaname"
								.equals(grandsonNode.getNodeName())) {
							String areaname = grandsonNode.getTextContent();
							info.setAreaname(areaname);
						} else if ("srvname".equals(grandsonNode.getNodeName())) {
							String srvname = grandsonNode.getTextContent();
							info.setSrvname(srvname);
						}
					}
				}
			}
			return info;
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

	// public static void main(String[] args){
	// StringBuilder sb = new StringBuilder();
	// sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
	// sb.append("<response>");
	// sb.append("<apiresult>");
	// sb.append("<resultcode>0</resultcode>");
	// sb.append("<resultdescription>购卡成功</resultdescription>");
	// sb.append("</apiresult>");
	// sb.append("<buycard>");
	// sb.append("<merchantid>1001</merchantid>");
	// sb.append("<productid>101</productid>");
	// sb.append("<producttype>直充</producttype>");
	// sb.append("<buynumber>10</buynumber>");
	// sb.append("<buyprice>0.94</buyprice>");
	// sb.append("<orderno>TB2012072116580001</orderno>");
	// sb.append("<userip>202.101.172.35</userip>");
	// sb.append("<saleorderid>101044</saleorderid>");
	// sb.append("</buycard>");
	// sb.append("<chargeinfo>");
	// sb.append("<username>iamgoodboy</username>");
	// sb.append("<gamename>龙</gamename>");
	// sb.append("<areaname>华南电信二</areaname>");
	// sb.append("<srvname>朱元璋</srvname>");
	// sb.append("</chargeinfo>");
	// sb.append("</response>");
	//
	// String xmlStr = sb.toString().trim();
	// parseTrade(xmlStr);
	// }
}
